import { _decorator, Component, instantiate, log, Node, Prefab, Sprite, v3, Vec3 } from 'cc';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { Carriage } from './Carriage';
import { Chariot } from './Chariot';
import { WeaponHP } from '../AshFramework/Datas/GameConstant';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

const carriagePos: Vec3[] = [v3(0, 0), v3(46, 115), v3(-46, -115), v3(92, 230), v3(-92, -230)]
// const carriageIndex: number[] = [8, 99, 7, 99];
// const carriageIndex: number[] = [3, 2, 4, 1, 5];

@ccclass('CarriageLayer')
export class CarriageLayer extends Component {
    @property(Prefab)
    CarriagePrefab: Prefab = null;

    private _arrCarriage: Array<Carriage> = new Array();
    private _type = 0;
    private _curWeaponCount: number = 0;

    private _singleHP: number = 0;
    private _totalHP: number = 0;
    private _harm: number = 0;//残留伤害


    get CarriageCount() {
        return this._arrCarriage.length;
    }

    init(type: number) {
        this._type = type;
        this._singleHP = Math.floor(WeaponHP[GameManager.Instance.userData.epoch][type - 1] * (1 + GameManager.Instance.userData.propertyAmplification.WEAPON_HP / 100));//属性赋值
        let carriage: Node = instantiate(this.CarriagePrefab);
        carriage.parent = this.node;
        carriage.setPosition(carriagePos[this._arrCarriage.length]);
        let ts: Carriage = carriage.getComponent(Carriage);
        ts.initCarriage(type);
        this._arrCarriage.push(ts);
    }

    creatorWeapon() {
        if (this._curWeaponCount == 45) return;
        if (this._curWeaponCount == 9 || this._curWeaponCount == 27) {
            this.creatorCarriage();
        }
        let index: number = Math.floor(this._curWeaponCount / 9);
        if (index < 1) {
            this._arrCarriage[index].creatorWeapon();
            this._curWeaponCount++;
        } else if (index < 3) {
            if (this._curWeaponCount % 2 == 1) {
                this._arrCarriage[1].creatorWeapon();
            } else {
                this._arrCarriage[2].creatorWeapon();
            }
            this._curWeaponCount++;
        } else if (index < 5) {
            if (this._curWeaponCount % 2 == 1) {
                this._arrCarriage[3].creatorWeapon();
            } else {
                this._arrCarriage[4].creatorWeapon();
            }
            this._curWeaponCount++;
        }
        this._totalHP += this._singleHP;
    }

    creatorCarriage() {
        if (this._arrCarriage.length == 5) return;

        let carriage: Node = instantiate(this.CarriagePrefab);
        carriage.parent = this.node;
        carriage.setPosition(carriagePos[this._arrCarriage.length]);
        carriage.setSiblingIndex(0);//往前面排
        let ts: Carriage = carriage.getComponent(Carriage);
        ts.initCarriage(this._type);
        this._arrCarriage.push(ts);

        let carriage2: Node = instantiate(this.CarriagePrefab);
        carriage2.parent = this.node;
        carriage2.setPosition(carriagePos[this._arrCarriage.length]);
        carriage2.setSiblingIndex(99);//往后面排
        let ts2: Carriage = carriage2.getComponent(Carriage);
        ts2.initCarriage(this._type);
        this._arrCarriage.push(ts2);

    }

    removeCarriage() {
        this._arrCarriage.pop().node.destroy();
        this._arrCarriage.pop().node.destroy();

    }

    removeWeapon() {
        this._curWeaponCount--;
        let index: number = Math.floor(this._curWeaponCount / 9);
        if (index < 1) {
            this._arrCarriage[0].removeWeapon();
        } else if (index < 3) {
            if (this._curWeaponCount % 2 == 0) {
                this._arrCarriage[1].removeWeapon();
            } else {
                this._arrCarriage[2].removeWeapon();
            }
        } else if (index < 5) {
            if (this._curWeaponCount % 2 == 0) {
                this._arrCarriage[3].removeWeapon();
            } else {
                this._arrCarriage[4].removeWeapon();
            }
        }

        if (this._curWeaponCount == 0) {
            this.reomveSelf();
            return;
        } else if (this._curWeaponCount == 9 || this._curWeaponCount == 27) {
            this.removeCarriage();
            return;
        }
    }

    beHit(harm: number) {
        this._totalHP -= harm;
        const count = Math.floor((this._harm + harm) / this._singleHP);
        if (count < 1) {
            this._harm += harm;
        } else {
            this._harm = harm % this._singleHP;
        }
        for (let i = 0; i < count; i++) {
            this.removeWeapon();
        }
    }

    reomveSelf() {
        Chariot.Instance.removeLayer(this);
        // this.node.removeAllChildren();
        this._arrCarriage.length = 0;
        this._singleHP = 0;
        this._totalHP = 0;
        this._harm = 0;
    }

}


